MaxRetryError: HTTPConnectionPool: Max retries exceeded (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))
MaxRetryError: HTTPConnectionPool: Max retries exceeded (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))
我有一个question:I想测试“select”和“输入”。我可以像下面的代码那样写吗:
原码:
12 class Sinaselecttest(unittest.TestCase):
13
14 def setUp(self):
15 binary = FirefoxBinary('/usr/local/firefox/firefox')
16 self.driver = webdriver.Firefox(firefox_binary=binary)
17
18 def test_select_in_sina(self):
19 driver = self.driver
20 driver.get("https://www.sina.com.cn/")
21 try:
22 WebDriverWait(driver,30).until(
23 ec.visibility_of_element_located((By.XPATH,"/html/body/div[9]/div/div[1]/form/div[3]/input"))
24 )
25 finally:
26 driver.quit()
# #测试select功能
27 select=Select(driver.find_element_by_xpath("//*[@id='slt_01']")).select_by_value("微博")
28 element=driver.find_element_by_xpath("/html/body/div[9]/div/div[1]/form/div[3]/input")
29 element.send_keys("杨幂")
30 driver.find_element_by_xpath("/html/body/div[9]/div/div[1]/form/input").click()
31 driver.implicitly_wait(5)
32 def tearDown(self):
33 self.driver.close()
我想测试Selenium "select" function.so 我选择新浪网站 select 一个选项,然后输入文本 textarea.then 搜索它。但是当我 运行这个测试,它有错误:
Traceback (most recent call last):
File "test_sina_select.py", line 32, in tearDown
self.driver.close()
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 688, in close
self.execute(Command.CLOSE)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 376, in execute
return self._request(command_info[0], url, body=data)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 399, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File "/usr/lib/python2.7/site-packages/urllib3/request.py", line 68, in request
**urlopen_kw)
File "/usr/lib/python2.7/site-packages/urllib3/request.py", line 81, in request_encode_url
return self.urlopen(method, url, **urlopen_kw)
File "/usr/lib/python2.7/site-packages/urllib3/poolmanager.py", line 247, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 617, in urlopen
release_conn=release_conn, **response_kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 617, in urlopen
release_conn=release_conn, **response_kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 617, in urlopen
release_conn=release_conn, **response_kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 597, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/lib/python2.7/site-packages/urllib3/util/retry.py", line 271, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=51379): Max retries exceeded with url: /session/2e64d2a1-3c7f-4221-96fe-9d0b1c102195/window (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))
----------------------------------------------------------------------
Ran 1 test in 72.106s
谁能告诉我为什么?谢谢
这个错误信息...
MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=51379): Max retries exceeded with url: /session/2e64d2a1-3c7f-4221-96fe-9d0b1c102195/window (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))
...意味着调用 self.driver.close()
方法失败引发 MaxRetryError.
几件事:
首先,根据讨论 max-retries-exceeded exceptions are confusing,traceback 有点误导。为方便用户,请求包装了异常。原始异常是显示的消息的一部分。
请求从不重试(它为 urllib3 的 HTTPConnectionPool
设置 retries=0
),因此如果没有 MaxRetryError[=,错误会更加规范100=] 和 HTTPConnectionPool 关键字。所以理想的 Traceback 应该是:
ConnectionError(<class 'socket.error'>: [Errno 1111] Connection refused)
但是@sigmavirus24 在他的 comment 中再次提到 ...包装这些异常会带来很好的 API 但调试体验很差...
推进计划是尽可能向下遍历到最低级别的异常并使用它。
最后,通过重新表述一些与实际连接被拒绝错误无关的异常来解决这个问题。
解决方案
甚至在 tearDown(self)
中的 self.driver.close()
被调用之前,test_select_in_sina(self)
中的 try{} 块包含 finally{ } 你调用了 driver.quit()
用于调用 /shutdown 端点,随后 Web 驱动程序和客户端实例被完全销毁,关闭所有 pages/tabs/windows。因此不再存在连接。
You can find a couple of relevant detailed discussion in:
在这种情况下,当您调用 self.driver.close()
时,python 客户端无法找到任何活动连接来启动关闭。因此您会看到错误。
所以一个简单的解决方案是删除行 driver.quit()
即删除 finally
块。
tl;博士
根据Selenium 3.14.1的发行说明:
* Fix ability to set timeout for urllib3 (#6286)
合并是:repair urllib3 can't set timeout!
结论
升级到 Selenium 3.14.1 后,您将能够设置超时并查看规范的 Tracebacks 并且能够采取必要的行动。
参考资料
几个相关参考资料:
刚遇到同样的问题。解决方案是使用递归脚本更改文件夹的所有者。在我的例子中,文件夹有 root:root
owner:group,我需要将其更改为 ubuntu:ubuntu
。
解法: sudo chown -R ubuntu:ubuntu /path-to-your-folder
Use Try and catch block to find exceptions
try:
r = requests.get(url)
except requests.exceptions.Timeout:
#Message
except requests.exceptions.TooManyRedirects:
#Message
except requests.exceptions.RequestException as e:
#Message
raise SystemExit(e)
我有一个question:I想测试“select”和“输入”。我可以像下面的代码那样写吗: 原码:
12 class Sinaselecttest(unittest.TestCase):
13
14 def setUp(self):
15 binary = FirefoxBinary('/usr/local/firefox/firefox')
16 self.driver = webdriver.Firefox(firefox_binary=binary)
17
18 def test_select_in_sina(self):
19 driver = self.driver
20 driver.get("https://www.sina.com.cn/")
21 try:
22 WebDriverWait(driver,30).until(
23 ec.visibility_of_element_located((By.XPATH,"/html/body/div[9]/div/div[1]/form/div[3]/input"))
24 )
25 finally:
26 driver.quit()
# #测试select功能
27 select=Select(driver.find_element_by_xpath("//*[@id='slt_01']")).select_by_value("微博")
28 element=driver.find_element_by_xpath("/html/body/div[9]/div/div[1]/form/div[3]/input")
29 element.send_keys("杨幂")
30 driver.find_element_by_xpath("/html/body/div[9]/div/div[1]/form/input").click()
31 driver.implicitly_wait(5)
32 def tearDown(self):
33 self.driver.close()
我想测试Selenium "select" function.so 我选择新浪网站 select 一个选项,然后输入文本 textarea.then 搜索它。但是当我 运行这个测试,它有错误:
Traceback (most recent call last):
File "test_sina_select.py", line 32, in tearDown
self.driver.close()
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 688, in close
self.execute(Command.CLOSE)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 376, in execute
return self._request(command_info[0], url, body=data)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 399, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File "/usr/lib/python2.7/site-packages/urllib3/request.py", line 68, in request
**urlopen_kw)
File "/usr/lib/python2.7/site-packages/urllib3/request.py", line 81, in request_encode_url
return self.urlopen(method, url, **urlopen_kw)
File "/usr/lib/python2.7/site-packages/urllib3/poolmanager.py", line 247, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 617, in urlopen
release_conn=release_conn, **response_kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 617, in urlopen
release_conn=release_conn, **response_kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 617, in urlopen
release_conn=release_conn, **response_kw)
File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 597, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/lib/python2.7/site-packages/urllib3/util/retry.py", line 271, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=51379): Max retries exceeded with url: /session/2e64d2a1-3c7f-4221-96fe-9d0b1c102195/window (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))
----------------------------------------------------------------------
Ran 1 test in 72.106s
谁能告诉我为什么?谢谢
这个错误信息...
MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=51379): Max retries exceeded with url: /session/2e64d2a1-3c7f-4221-96fe-9d0b1c102195/window (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))
...意味着调用 self.driver.close()
方法失败引发 MaxRetryError.
几件事:
首先,根据讨论 max-retries-exceeded exceptions are confusing,traceback 有点误导。为方便用户,请求包装了异常。原始异常是显示的消息的一部分。
请求从不重试(它为 urllib3 的
HTTPConnectionPool
设置retries=0
),因此如果没有 MaxRetryError[=,错误会更加规范100=] 和 HTTPConnectionPool 关键字。所以理想的 Traceback 应该是:ConnectionError(<class 'socket.error'>: [Errno 1111] Connection refused)
但是@sigmavirus24 在他的 comment 中再次提到 ...包装这些异常会带来很好的 API 但调试体验很差...
推进计划是尽可能向下遍历到最低级别的异常并使用它。
最后,通过重新表述一些与实际连接被拒绝错误无关的异常来解决这个问题。
解决方案
甚至在 tearDown(self)
中的 self.driver.close()
被调用之前,test_select_in_sina(self)
中的 try{} 块包含 finally{ } 你调用了 driver.quit()
用于调用 /shutdown 端点,随后 Web 驱动程序和客户端实例被完全销毁,关闭所有 pages/tabs/windows。因此不再存在连接。
You can find a couple of relevant detailed discussion in:
在这种情况下,当您调用 self.driver.close()
时,python 客户端无法找到任何活动连接来启动关闭。因此您会看到错误。
所以一个简单的解决方案是删除行 driver.quit()
即删除 finally
块。
tl;博士
根据Selenium 3.14.1的发行说明:
* Fix ability to set timeout for urllib3 (#6286)
合并是:repair urllib3 can't set timeout!
结论
升级到 Selenium 3.14.1 后,您将能够设置超时并查看规范的 Tracebacks 并且能够采取必要的行动。
参考资料
几个相关参考资料:
刚遇到同样的问题。解决方案是使用递归脚本更改文件夹的所有者。在我的例子中,文件夹有 root:root
owner:group,我需要将其更改为 ubuntu:ubuntu
。
解法: sudo chown -R ubuntu:ubuntu /path-to-your-folder
Use Try and catch block to find exceptions
try:
r = requests.get(url)
except requests.exceptions.Timeout:
#Message
except requests.exceptions.TooManyRedirects:
#Message
except requests.exceptions.RequestException as e:
#Message
raise SystemExit(e)